<?php
class PlanModel extends Action{
	//test
	//
	//############test
	public function add($dt){
		$info=collectinfo(__METHOD__,'$dt',array($dt));
		if(isset($dt)===false){return createarrerr('error_code','dt 不能为空',$info);}//防止NULL
		
		$plan=M('plan');
		$plan->data($dt)->add();

		$arr_plano=$this->getlastplano();$plano=$arr_plano['data'];

		return createarrok('ok',$plano,'',$info);
	}

	public function getlastplano(){
		$info=collectinfo(__METHOD__,'',array());
				
		$plan=M('plan');
		$plano=$plan->order('planid DESC')->find();

		return createarrok('ok',$plano,'',$info);
	}

	public function getplano_running($lineid){
		$info=collectinfo(__METHOD__,'$lineid',array($lineid));
				
		$plan=M('plan');
		$plano=$plan->join('tb_line ON f_plan_lineid=lineid')->join('tb_item ON f_plan_itemid=itemid')->where('planstat=2 AND f_plan_lineid='.$lineid)->find();

		return createarrok('ok',$plano,'',$info);
	}

	public function getplanlsofnotpsandnotfinish(){
		$info=collectinfo(__METHOD__,'',array());
				
		$plan=M('plan');

		$planls=$plan->where('planstat<>2 AND planstat<>3')->select();

		return createarrok('ok',$planls,'',$info);
	}

	public function getplanlsofnotstartandnotready(){
		$info=collectinfo(__METHOD__,'',array());
				
		$plan=M('plan');

		$planls=$plan->where('planstat<>0 AND planstat<>1')->select();

		return createarrok('ok',$planls,'',$info);
	}

	public function get0statplanlsbylineidandtmandbanciid($lineid,$tm,$banciid){
		$info=collectinfo(__METHOD__,'$lineid,$tm,$banciid',array($lineid,$tm,$banciid));
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL
		if(isset($tm)===false){return createarrerr('error_code','tm 不能为空',$info);}//防止NULL
		if(isset($banciid)===false){return createarrerr('error_code','banciid 不能为空',$info);}//防止NULL
		
		$plan=M('plan');

		$planls=$plan->join('tb_line ON f_plan_lineid=lineid')->join('tb_item ON f_plan_itemid=itemid')->join('tb_banci ON f_plan_banciid=banciid')->where("f_plan_lineid=".$lineid." AND plantm='".$tm."' AND f_plan_banciid=".$banciid.' AND planstat=0')->order('planodr ASC')->select();

		return createarrok('ok',$planls,'',$info);
	}

	public function getplanlsbylineidandtmandbanciid($lineid,$tm,$banciid){
		$info=collectinfo(__METHOD__,'$lineid,$tm,$banciid',array($lineid,$tm,$banciid));
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL
		if(isset($tm)===false){return createarrerr('error_code','tm 不能为空',$info);}//防止NULL
		if(isset($banciid)===false){return createarrerr('error_code','banciid 不能为空',$info);}//防止NULL
		
		$plan=M('plan');

		$planls=$plan->join('tb_line ON f_plan_lineid=lineid')->join('tb_item ON f_plan_itemid=itemid')->join('tb_banci ON f_plan_banciid=banciid')->where("f_plan_lineid=".$lineid." AND plantm='".$tm."' AND f_plan_banciid=".$banciid)->order('planodr ASC')->select();

		return createarrok('ok',$planls,'',$info);
	}

	public function getplanlsbylineid($lineid){
		$info=collectinfo(__METHOD__,'$lineid',array($lineid));
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL
		
		$plan=M('plan');

		$planls=$plan->join('tb_line ON f_plan_lineid=lineid')->join('tb_item ON f_plan_itemid=itemid')->join('tb_banci ON f_plan_banciid=banciid')->where("f_plan_lineid=".$lineid)->order('planstat DESC,planodr ASC')->select();

		return createarrok('ok',$planls,'',$info);
	}

	public function autoquedingnext($lineid){
		$info=collectinfo(__METHOD__,'$lineid',array($lineid));
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL
		
		$plan=M('plan');

		$plano=$plan->join('tb_line ON f_plan_lineid=lineid')->join('tb_item ON f_plan_itemid=itemid')->where("f_plan_lineid=".$lineid.' AND planstat=0')->order('planid DESC')->find();
		$dt=array('planstat'=>1);
		$plan->where('planid='.$plano['planid'])->setField($dt);

		return createarrok('ok',$plano,'',$info);
	}

	public function getquedingplano($lineid){
		$info=collectinfo(__METHOD__,'$lineid',array($lineid));
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL
		
		$plan=M('plan');

		$plano=$plan->join('tb_line ON f_plan_lineid=lineid')->join('tb_item ON f_plan_itemid=itemid')->where("f_plan_lineid=".$lineid.' AND planstat=1')->find();

		return createarrok('ok',$plano,'',$info);
	}

	

	public function getplanlsbylineid_nocomplete($lineid){
		$info=collectinfo(__METHOD__,'$lineid',array($lineid));
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL
		
		$plan=M('plan');

		$planls=$plan->join('tb_line ON f_plan_lineid=lineid')->join('tb_item ON f_plan_itemid=itemid')->join('tb_banci ON f_plan_banciid=banciid')->where("f_plan_lineid=".$lineid.' AND planstat<>4')->order('planstat DESC,plantm ASC')->select();

		$planlsnw=array();
		foreach($planls as $planv){
			$planstat=$planv['planstat'];
			if($planstat==0){$planstatdesc='未开始';}else
			if($planstat==1){$planstatdesc='已确认';}else
			if($planstat==2){$planstatdesc='运行中';}else
			if($planstat==4){$planstatdesc='已完成';}
			$planv['planstatdesc']=$planstatdesc;
			array_push($planlsnw, $planv);
		}

		return createarrok('ok',$planlsnw,'',$info);
	}

	public function getplanobyplanid($planid){
		$info=collectinfo(__METHOD__,'$planid',array($planid));
		
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL

		$plan=M('plan');
		$plano=$plan->where('planid='.$planid)->find();

		return createarrok('ok',$plano,'',$info);
	}


	public function changeplanstat($planid,$lineid){
		$info=collectinfo(__METHOD__,'$planid,$lineid',array($planid,$lineid));
		
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL


		$str='';
		$plan=M('plan');$ps=M('ps');

		$planls_running=$plan->where('f_plan_lineid='.$lineid.' AND planstat=2')->select();
		$planls_queding=$plan->where('f_plan_lineid='.$lineid.' AND planstat=1')->select();
		if(!$planls_running){
			$dt['planstat']=2;
			$dt['planstarttm']=tm();
			$plan->where('planid='.$planid)->setField($dt);
			$dtII['f_ps_psstatid']=3;
			$ps->where('f_ps_planid='.$planid)->setField($dtII);
		}else if($planls_running&&$planls_queding){
			$str='已经有一个计划在运行，一个计划确定中，不能再确定';
		}else{
			$dt['planstat']=1;
			$plan->where('planid='.$planid)->setField($dt);
		}

		

		return createarrok('ok',$str,'',$info);
	}
	

	public function queding($planid){
		$info=collectinfo(__METHOD__,'$planid',array($planid));
		
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL

		$plan=M('plan');
		$dt=array('planstat'=>1);
		$plan->where('planid='.$planid)->setField($dt);

		return createarrok('ok',$data,'',$info);
	}

	public function getplanlsbyplantmandbanciidwithstat0($plantm,$banciid){
		$info=collectinfo(__METHOD__,'$plantm,$banciid',array($plantm,$banciid));
		
		if(isset($plantm)===false){return createarrerr('error_code','plantm 不能为空',$info);}//防止NULL
		if(isset($banciid)===false){return createarrerr('error_code','banciid 不能为空',$info);}//防止NULL

		$plan=M('plan');
		$planls=$plan->where("plantm='".$plantm."' AND f_plan_banciid=".$banciid." AND planstat=0")->select();

		return createarrok('ok',$planls,'',$info);
	}

	public function changeplanodr($planid){
		$info=collectinfo(__METHOD__,'$planid',array($planid));
		
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		
		 //第一步获取该plano，查看和他一起的planls
      	$arr_plano=$this->getplanobyplanid($planid);$plano=$arr_plano['data'];
      	$arr_planls=$this->get0statplanlsbylineidandtmandbanciid($plano['f_plan_lineid'],$plano['plantm'],$plano['f_plan_banciid']);$planls=$arr_planls['data'];
      	$odr_min=1;
      	for($i=0;$i<count($planls);$i++){
      		$planv=$planls[$i];
      		if($i==0){$odr_min=$planv['planodr'];}
      		if($planv['planodr']<$plano['planodr']){
      			$get['planodr']=$planv['planodr']+1;
      			$this->mdf($get,$planv['planid']);
      		}
      		
      	}
      	$get=array();
      	$get['planodr']=$odr_min;
      	$get['planstat']=1;
      	$this->mdf($get,$planid);

		
		return createarrok('ok',$data,'',$info);
	}

	public function mdf($get,$planid){
		$info=collectinfo(__METHOD__,'$get,$planid',array($get,$planid));
		if(isset($get)===false){return createarrerr('error_code','get 不能为空',$info);}//防止NULL
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL

		$plan=M('plan');
		$plan->where('planid='.$planid)->setField($get);

		return createarrok('ok',$data,'',$info);
	}

	public function jiapei($planid,$jiapeishuliang){
		$info=collectinfo(__METHOD__,'$planid,$jiapeishuliang',array($planid,$jiapeishuliang));
		
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		if(isset($jiapeishuliang)===false){return createarrerr('error_code','jiapeishuliang 不能为空',$info);}//防止NULL
		
		$arr_plano=$this->getplanobyplanid($planid);$plano=$arr_plano['data'];
		
		$dt=array('planvolumebox'=>($plano['planvolumebox']+$jiapeishuliang));
		$plan=M('plan');
		$plan->where('planid='.$planid)->setField($dt);



		return createarrok('ok',$data,'',$info);
	}

	public function wancheng($planid){
		$info=collectinfo(__METHOD__,'$planid',array($planid));
		
		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		
		$dt=array('planstat'=>4);
		$plan=M('plan');
		$plan->where('planid='.$planid)->setField($dt);



		return createarrok('ok',$data,'',$info);
	}

	public function getplanobylineidanditemidandrunning($lineid,$itemid){
		$info=collectinfo(__METHOD__,'$lineid,$itemid',array($lineid,$itemid));
		
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL
		if(isset($itemid)===false){return createarrerr('error_code','itemid 不能为空',$info);}//防止NULL

		$plan=M('plan');
		$plano=$plan->where('planstat=2 AND f_plan_lineid='.$lineid.' AND f_plan_itemid='.$itemid)->find();

		return createarrok('ok',$plano,'',$info);
	}
	
	public function addplanrealvolume($planid,$planrealvolume){
		$info=collectinfo(__METHOD__,'$planid,$planrealvolume',array($planid,$planrealvolume));

		if(isset($planid)===false){return createarrerr('error_code','planid 不能为空',$info);}//防止NULL
		
		if(isset($planrealvolume)===false){return createarrerr('error_code','planrealvolume 不能为空',$info);}//防止NULL
		
		$dt=array('planrealvolume'=>$planrealvolume);
		$plan=M('plan');
		$plan->where('planid='.$planid)->setField($dt);



		return createarrok('ok',$data,'',$info);
	}

	public function getpreplanodr($plantm,$lineid,$banciid){
		$info=collectinfo(__METHOD__,'$plantm,$lineid,$banciid',array($plantm,$lineid,$banciid));

		if(isset($plantm)===false){return createarrerr('error_code','plantm 不能为空',$info);}//防止NULL
		if(isset($lineid)===false){return createarrerr('error_code','lineid 不能为空',$info);}//防止NULL
		if(isset($banciid)===false){return createarrerr('error_code','banciid 不能为空',$info);}//防止NULL
		
		
		$plan=M('plan');
		$plano=$plan->where("plantm='".$plantm."' AND f_plan_lineid=".$lineid." AND f_plan_banciid=".$banciid)->order('planodr DESC')->find();



		return createarrok('ok',$plano['planodr'],'',$info);
	}

	public function getplanobyplantmandplanodr($plantm,$planodr){
		$info=collectinfo(__METHOD__,'$plantm,$planodr',array($plantm,$planodr));

		if(isset($plantm)===false){return createarrerr('error_code','plantm 不能为空',$info);}//防止NULL
		
		if(isset($planodr)===false){return createarrerr('error_code','planodr 不能为空',$info);}//防止NULL
		
		
		$plan=M('plan');
		$plano=$plan->where("plantm='".$plantm."' AND planodr='".$planodr."'")->find();



		return createarrok('ok',$plano,'',$info);
	}
} 
?>